package com.eshop.modules.system.rest;

import java.lang.invoke.SerializedLambda;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eshop.modules.system.domain.Dept;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.util.Iterator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import java.util.HashSet;
import java.util.Set;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.exception.BadRequestException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.system.domain.Menu;
import com.eshop.modules.system.service.dto.UserDto;
import org.springframework.http.HttpStatus;
import com.eshop.utils.SecurityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import com.eshop.modules.aop.ForbidSubmit;
import java.io.IOException;
import java.util.List;
import com.eshop.modules.system.service.dto.MenuDto;
import com.eshop.modules.system.service.dto.MenuQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.system.service.RoleService;
import com.eshop.modules.system.service.UserService;
import com.eshop.modules.system.service.MenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "系统：菜单管理" })
@RestController
@RequestMapping({ "/api/menus" })
public class MenuController
{
    private final MenuService menuService;
    private final UserService userService;
    private final RoleService roleService;
    private final IGenerator generator;
    private static final String ENTITY_NAME = "menu";
    
    public MenuController(final MenuService menuService, final UserService userService, final RoleService roleService, final IGenerator generator) {
        this.menuService = menuService;
        this.userService = userService;
        this.roleService = roleService;
        this.generator = generator;
    }
    
    @ForbidSubmit
    @Log("导出菜单数据")
    @ApiOperation("导出菜单数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('menu:list')")
    public void download(final HttpServletResponse response, final MenuQueryCriteria criteria) throws IOException {
        this.menuService.download(this.generator.convert((List)this.menuService.queryAll(criteria), (Class)MenuDto.class), response);
    }
    
    @ApiOperation("获取前端所需菜单")
    @GetMapping({ "/build" })
    public ResponseEntity<Object> buildMenus() {
        final UserDto user = this.userService.findByName(SecurityUtils.getUsername());
        final List<MenuDto> menuDtoList = this.menuService.findByRoles(this.roleService.findByUsersId(user.getId()));
        final List<MenuDto> menuDtos = (List<MenuDto>) this.menuService.buildTree(menuDtoList).get("content");
        return new ResponseEntity(this.menuService.buildMenus(menuDtos), HttpStatus.OK);
    }
    
    @ApiOperation("返回全部的菜单")
    @GetMapping({ "/tree" })
    @PreAuthorize("@el.check('menu:list','roles:list')")
    public ResponseEntity<Object> getMenuTree() {
        return new ResponseEntity(this.menuService.getMenuTree(this.menuService.findByPid(0L)), HttpStatus.OK);
    }
    
    @Log("查询菜单")
    @ApiOperation("查询菜单")
    @GetMapping
    @PreAuthorize("@el.check('menu:list')")
    public ResponseEntity<Object> getMenus(final MenuQueryCriteria criteria) {
        final List<MenuDto> menuDtoList = (List<MenuDto>)this.generator.convert((List)this.menuService.queryAll(criteria), (Class)MenuDto.class);
        return new ResponseEntity(this.menuService.buildTree(menuDtoList), HttpStatus.OK);
    }
    
    @ForbidSubmit
    @Log("新增菜单")
    @ApiOperation("新增菜单")
    @PostMapping
    @PreAuthorize("@el.check('menu:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final Menu resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new menu cannot already have an ID");
        }
        return new ResponseEntity(this.menuService.create(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("修改菜单")
    @ApiOperation("修改菜单")
    @PutMapping
    @PreAuthorize("@el.check('menu:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final Menu resources) {
        this.menuService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除菜单")
    @ApiOperation("删除菜单")
    @DeleteMapping
    @PreAuthorize("@el.check('menu:del')")
    public ResponseEntity<Object> delete(@RequestBody final Set<Long> ids) {
        Set<Menu> menuSet = new HashSet<Menu>();
        for (final Long id : ids) {
            final List<Menu> menuList = this.menuService.findByPid(id);
            menuSet.add((Menu)this.menuService.getOne( Wrappers.<Menu>lambdaQuery().eq(Menu::getId, id)));
            menuSet = this.menuService.getDeleteMenus(menuList, menuSet);
        }
        this.menuService.delete(menuSet);
        return new ResponseEntity(HttpStatus.OK);
    }
}
